﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zmc.Apparatu;
using B.S.Sterilizationcenter.Writer.API.FileNlog;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Zmc.Apparatu
{
    public class CreateApparatusCommendHadnlers : IRequestHandler<CreateApparatusCommends, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        private readonly Nolgs nolgs;

        public CreateApparatusCommendHadnlers(ISqlSugarClient db, IMapper mapper, Nolgs nolgs)
        {
            this.db = db;
            this.mapper = mapper;
            this.nolgs = nolgs;
        }

        /// <summary>
        /// 创建器械数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>1

        public async Task<ApiResult<int>> Handle(CreateApparatusCommends request, CancellationToken cancellationToken)
        {
            var startTime = DateTime.Now;
            try
            {
                ApiResult<int> result = new ApiResult<int>();
                var data = mapper.Map<Apparatus>(request);
              //  data.ApparatusNo = "100" + DateTime.Now.ToString("yyyyMMddHHmmss");
                data.IsDeletes = false;
                result.Data = await db.Insertable(data).ExecuteCommandAsync();
                if (result.Data > 0)
                {
                    result.Code = ApiEnums.添加成功;
                    result.Msg = "添加器械数据";
                }
                else
                {
                    result.Code = ApiEnums.添加失败;
                    result.Msg = "添加器械数据失败";
                }
                return result;
            }

            catch (Exception)
            {

                throw;
            }
           
        }

    }
}
